<template>
  <div
    class="yd-switch"
    :style="{backgroundColor:modelValue==activeValue||value==activeValue?activeColor:inactiveColor}"
    @click="$emit('update:modelValue',modelValue==activeValue?inactiveValue:activeValue)"
  >
    <span
      class="yd-core"
      :style="{transform: `translateX(${modelValue==activeValue||value==activeValue?22:0}px)`}"
    ></span>
  </div>
</template>

<script>
import { watch } from "vue";
export default {
  name: "yd-switch",
  props: {
    modelValue: null,
    value: null,
    activeValue: {
      type: null,
      default: true
    },
    inactiveValue: {
      type: null,
      default: false
    },
    activeColor: {
      type: String,
      default: "#409eff"
    },
    inactiveColor: {
      type: String,
      default: "#dcdfe6"
    }
  },
  setup(props, ctx) {
    watch(
      () => props.modelValue,
      val => {
        ctx.emit("change", val);
      }
    );
    watch(
      () => props.value,
      val => {
        ctx.emit("change", val);
      }
    );
  }
};
</script>

<style lang="scss" scoped>
.yd-switch {
  width: 40px;
  height: 20px;
  padding: 2px 1px;
  border-radius: 10px;
  box-sizing: border-box;
  transition: all 0.3s;
  cursor: pointer;
  .yd-core {
    display: block;
    width: 16px;
    height: 16px;
    background-color: #fff;
    border-radius: 50%;
    transition: all 0.3s;
  }
}
</style>